package com.zy.utils.data;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.zy.utils.CanvasUtils;

//默认方形
public class Point {

    private float originX;  //原始x
    private float originY;  //原始Y
    protected final String v; //数据
    protected final float per; //长宽或者R
    protected int index; //下标
    protected boolean showIndex; //下标
    private boolean circle; //方形和圆形


    private float offX;  //偏移
    private float offY;  //偏移
    private float percentage = 1f;  //偏移百分比

    private boolean sorted = false;


    public Point(int originX, int originY, String v, int per, int index) {
        this.originX = originX;
        this.originY = originY;
        this.v = v;
        this.per = per;
        this.index = index;
    }

    public Point(Builder builder) {
        this.originX = builder.originX;
        this.originY = builder.originY;
        this.v = builder.v;
        this.per = builder.per;
        this.index = builder.index;
        this.showIndex = builder.showIndex;
        this.circle = builder.circle;
    }

    public float getCenterX() {
        return originX + offX * percentage;
    }


    public float getOriginX() {
        return originX;
    }

    public float getCenterY() {
        return originY + offY * percentage;
    }

    public float getOriginY() {
        return originY;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    public void updateOff(float x, float y) {
        offX = x;
        offY = y;
    }

    public void setSorted() {
        this.sorted = !sorted;
    }

    public void resetOff() {
        offX = 0;
        offY = 0;
        percentage = 1f;
    }

    public void setPoint(float x, float y) {
        this.originX = x;
        this.originY = y;
    }

    public int getIndex() {
        return index;
    }


    public void exchangeIndex(Point point) {
        if (point == null) {
            return;
        }
        int tmp = index;
        index = point.index;
        point.index = tmp;
    }

    public void draw(Canvas canvas, Paint paint, Paint backgroundPaint) {
        if (circle) {
            drawCircle(canvas, backgroundPaint);
        } else {
            drawBackground(canvas, backgroundPaint);
        }
        drawV(canvas, paint);

        if (showIndex) {
            drawIndex(canvas, paint);
        }
    }

    protected void drawV(Canvas canvas, Paint paint) {
        paint.setColor(Color.RED);
        CanvasUtils.drawTextCenter(canvas, paint, getCenterX(), getCenterY(), String.valueOf(v));
    }

    protected void drawIndex(Canvas canvas, Paint paint) {
        float old = paint.getTextSize();
        paint.setTextSize(24);
        CanvasUtils.drawTextCenter(canvas, paint, getCenterX() + per, getCenterY() + per, String.valueOf(index));
        paint.setTextSize(old);
    }

    protected void drawBackground(Canvas canvas, Paint paint) {
        //drawRect(canvas,paint);
        drawRoundRect(canvas, paint);
    }

    public void drawRect(Canvas canvas, Paint paint) {
        float x = getCenterX();
        float y = getCenterY();
        canvas.drawRect(x - per, y - per, x + per, y + per, paint);
    }

    public void drawCircle(Canvas canvas, Paint paint) {
        float x = getCenterX();
        float y = getCenterY();
        if (sorted) {
            paint.setColor(Color.GREEN);
        } else {
            paint.setColor(Color.BLUE);
        }

        canvas.drawCircle(x, y, per, paint);
    }

    public void drawRoundRect(Canvas canvas, Paint paint) {
        float x = getCenterX();
        float y = getCenterY();
        if (sorted) {
            paint.setColor(Color.GREEN);
        } else {
            paint.setColor(Color.BLUE);
        }
        canvas.drawRoundRect(x - per, y - per, x + per, y + per, per / 4, per / 4, paint);
    }

    public boolean isRange(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        float originX = getCenterX();
        float originY = getCenterY();
        return x >= originX - per && x <= originX + per && y >= originY - per && y <= originY + per;
    }


    public enum Type {
        DEFAULT, DEFAULT_INDEX, ROUND, ROUND_INDEX, SUB_SCRIPT,//下标
        ASSIST,//基数&计数
    }

    public static class Builder {


        private float originX;  //原始x
        private float originY;  //原始Y
        private String v; //数据
        private float per; //长宽或者R
        private int index; //下标
        private boolean showIndex; //是否显示下标

        private boolean circle; //方形和圆形

        public Builder() {
        }

        public Builder setOriginX(float originX) {
            this.originX = originX;
            return this;
        }

        public Builder setOriginY(float originY) {
            this.originY = originY;
            return this;
        }

        public Builder setV(String v) {
            this.v = v;
            return this;
        }

        public Builder setPer(float per) {
            this.per = per;
            return this;
        }

        public Builder setIndex(int index) {
            this.index = index;
            return this;
        }

        public Point build(Type type) {
            switch (type) {
                case SUB_SCRIPT:
                    return new SubScriptPoint(this);
                case ASSIST:
                    return new AssistPoint(this);
                case ROUND_INDEX:
                    showIndex = true;
                    circle = true;
                    return new Point(this);
                case ROUND:
                    showIndex = false;
                    circle = true;
                    return new Point(this);
                case DEFAULT_INDEX:
                    showIndex = true;
                case DEFAULT:
                    return new Point(this);
            }
            return new Point(this);
        }
    }
}
